<%@ page import="schoolsource.sql.SchoolDBSQL" %>
<%@ page import="schoolsource.util.Types" %>
<%@ page import="schoolsource.model.*" %>
<%@ page import="schoolsource.gui.FormFields" %>
<%@ page import="schoolsource.security.SecurityToken" %>
<%@ page import="java.text.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.ParsePosition" %>

<% SecurityToken st = (SecurityToken) request.getAttribute(FormFields.SESSION);
   String requestObject = (String) st.getValue(FormFields.TOKEN);
   int classnum = (new Integer((String) st.getValue(FormFields.CLASSNUM))).intValue();
   ClassContainer classC[] = (ClassContainer[]) st.getValue("classContainer");
   UserContainer studentC[] = (UserContainer[]) st.getValue("studentsInClass");
   String loginType = (String) request.getAttribute(FormFields.USRTYPE);
   String schoolName = (String) request.getAttribute(FormFields.SCHOOLNAME);

//   SimpleDateFormat Types.sdfMMDD = new SimpleDateFormat("MM-dd");
//   SimpleDateFormat sdfMMDDYYYYslash = new SimpleDateFormat("MM/dd/yyyy");
//   SimpleDateFormat sdfMMDDYYYY = new SimpleDateFormat("MMddyyyy");

   GregorianCalendar date = new GregorianCalendar();
   String dateString;
   if ((dateString = (String) st.removeValue(FormFields.ATTENDANCEDATE)) != null && !"".equals(dateString)) {
       date.setTime(Types.sdfMMDDYYYY.parse(dateString, new ParsePosition(0)));
   }
%>

<HTML>
  <HEAD>
      <TITLE>Class Attendance</TITLE>

      <SCRIPT language="JavaScript">

          function save() {
              document.forms.classattendance.<%= FormFields.LINKCLICKED %>.value = "Save Attendance";
              document.forms.classattendance.submit();
          }

          function cancel() {
              document.forms.classattendance.<%= FormFields.LINKCLICKED %>.value = "Cancel";
              document.forms.classattendance.submit();
          }

      </SCRIPT>
  </HEAD>
  <BODY>
      <FORM NAME="classattendance" METHOD="POST">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CURRENT %>" VALUE="<%= FormFields.SUBFRAME %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.TOKEN %>" VALUE="<%= requestObject%>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PARAMETER %>" VALUE="">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.LINKCLICKED %>" VALUE="">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CLASSNUM %>" VALUE="<%= classnum %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.SCHOOLNAME %>" VALUE="<%= schoolName %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.USRTYPE %>" VALUE="<%= loginType %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.ATTENDANCEDATE %>" VALUE="<%= dateString %>">
    <TABLE ALIGN="CENTER" BORDER="0">
      <TR>
        <TD>
          <CENTER><INPUT TYPE="BUTTON" NAME="<%= FormFields.BUTTONCLICKED %>" VALUE="Save Attendance" onClick="save();">
        </TD>
        <TD>
          <CENTER><INPUT TYPE="BUTTON" NAME="<%= FormFields.BUTTONCLICKED %>" VALUE="Cancel" onClick="cancel();"></CENTER>
        </TD>
      </TR>
    </TABLE>

    &nbsp;<br>
    <TABLE ALIGN="CENTER" BORDER="2">
      <TR>
      <%
      out.print("<TH COLSPAN=\"2\">"+ Types.sdfMMDD.format(date.getTime()) + "</TH>\n");
      %>
      </TR>
        <%
        if (studentC != null) {
            // this for loop runs thru the STUDENTS
            for (int i = 0; i < studentC.length; i++) {
                out.println("<TR>");
                out.println("<TD>" + studentC[i].getLastName() + ", " + studentC[i].getFirstName() + " </TD>");

                String id = studentC[i].getAssignedID().toString();

                String time = Types.sdfMMDDYYYY.format(date.getTime());
                String timeslash = Types.sdfMMDDYYYYslash.format(date.getTime());

                AttendanceContainer ac =
                     AttendanceContainer.getAttendanceContainer(id,
                                        classC[classnum].getClassAssignedID().toString(),
                                        timeslash,
                                        ((String) st.getValue(FormFields.SCHOOLNAME)));


                out.println("<TD><SELECT tabindex="+((i+1))+
                     " style=\"WIDTH: 100PX\" NAME=\"" +
                     SchoolDBSQL.USRASSIGNEDID + id +
                     SchoolDBSQL.ATNDDATE + time + "\">");
                if ("ABSENT".equals(ac.getType().toString())) {
                     out.println("<OPTION ></OPTION>");
                     out.println("<OPTION VALUE=\"ABSENT\" SELECTED>Absent</OPTION>");
                     out.println("<OPTION VALUE=\"TARDY\">Tardy</OPTION>");
                }
                else if ("TARDY".equals(ac.getType().toString())) {
                     out.println("<OPTION ></OPTION>");
                     out.println("<OPTION VALUE=\"ABSENT\">Absent</OPTION>");
                     out.println("<OPTION VALUE=\"TARDY\" SELECTED>Tardy</OPTION>");
                }
                else {
                     out.println("<OPTION SELECTED></OPTION>");
                     out.println("<OPTION VALUE=\"ABSENT\">Absent</OPTION>");
                     out.println("<OPTION VALUE=\"TARDY\">Tardy</OPTION>");
                }
                out.println("</SELECT></TD>");

                out.println("</TR>");
            }
        }
        %>
    </TABLE>
    &nbsp;<br>
    <TABLE ALIGN="CENTER" BORDER="0">
      <TR>
        <TD>
          <CENTER><INPUT TYPE="BUTTON" NAME="<%= FormFields.BUTTONCLICKED %>" VALUE="Save Attendance" onClick="save();">
        </TD>
        <TD>
          <CENTER><INPUT TYPE="BUTTON" NAME="<%= FormFields.BUTTONCLICKED %>" VALUE="Cancel" onClick="cancel();"></CENTER>
        </TD>
      </TR>
    </TABLE>
      </FORM>
  </BODY>
</HTML>